import { Component } from '@angular/core';
import { NavController,AlertController } from 'ionic-angular';

import { HTTP } from '@ionic-native/http';

import   $  from 'jquery';

import {Fort2Page} from "../fortpas2/fortpas2"


@Component({
  selector: 'page-fortpass',
  templateUrl: 'fortpass.html'
})
export class FortPage {
  
    userName:string; //用户名
    userMa:string;   //验证码
   
    public url:string ="http://118.31.79.20:8080/";//接口地址
    // public url:string ="http://192.168.1.114:8080/";
    public loginurl:string = this.url + "Stock/login/forgetPwd.do";//忘记密码接口
    public yanurl:string = this.url + "Stock/code/getCode.do";
  constructor(public navCtrl: NavController,private http: HTTP,private malert:AlertController) {

  }
 ionViewDidLoad(){
        // var autoRandom =document.querySelector(".yan");
                // autoRandom.innerHTML=result;
          this.ma();
    }
  // 更换验证码
  ma(){
    //   随机获取验证码
       this.http.get(this.yanurl, {}, {})
             .then(data => {
                //获取的数据
                var datass=JSON.parse(data.data);
                var result = datass.data; //验证码
                // alert("验证码"+datass.data);
               var autoRandom =document.querySelector(".yan");
               autoRandom.innerHTML=result;
            })
            .catch(error => {
                            
                 this.mWinds("网络异常");
                 
            });     
  }

  iphone(){
     // 手机号正则验证
      let regTel = /^0?1[3|4|5|8][0-9]\d{8}$/;
      if(!regTel.test(this.userName)){
         
          this.mWinds("您好，账号格式不正确!!");
          return false;
      }else{
          return true;
      }
  }

  yma(){
    let mas = $(".yan").text();
        if(this.userMa != mas){
           
            this.mWinds("验证码错误!!");
            return false;
        }else if(this.userMa == '' || this.userMa ==null){
           
           this.mWinds("验证码不能为空!!");
            return false;
        }else{
            return true;
        }

  }

  login(){
      
       if(this.userName == "" || this.userName == null ){
           
           this.mWinds("您好，账户和密码不能为空!");
            return false;
           
            
        }else if(this.iphone() && this.yma()){
                //请求
              this.http.post(this.loginurl,{"mobile":this.userName},{})   //接口。发送的数据
              .then(data => {
                  // console.log("请求成功");
                 let zhaungtai=JSON.parse(data.data);  //转换json数据

                //  alert("数据" + data.data);  //ok  取到数据
                //  alert("状态"+ zhaungtai.flag);  //true
        
                if(zhaungtai.flag == true){    //成功跳转等页面
                    //传参
                      this.navCtrl.push(Fort2Page,{items:this.userName});
                    
                }else{
                     this.mWinds("您好，用户名不存在!!");
                    return false;
                }
                
              }).catch(error => {
                 
                  this.mWinds("请求失败");
                 
              
              });
           
        } 
  }
  /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{       
        }
      }],
      
    });
  alert.present();

    }
    /**
     * 无按钮
     * @param msg 
     */
    mWinds(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,     
    });
  alert.present();

    }
}
